﻿using Oplug.CoolTower.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Oplug.CoolTower
{
    public partial class FormMain : MetroFramework.Forms.MetroForm
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            FormLogin formLogin = new FormLogin();
            formLogin.ShowDialog();
            LoadData();            
        }

        private void LoadData()
        {
            lableDate.Text = DateTime.Now.ToShortDateString();
            List<InputPara> dataSource = new List<InputPara>();
            string titles = "Water Flow  (循环水量）m3/h|Hot Water Temp (进水温度）℃|Cold Water Temp (出水温度）℃| Wet Bulb Temp  (湿球温度）℃|Heat Rejection (热负荷) kcal/hr|RANGE（温降）℃|APPROCH（逼近度）℃|Water-Air  Ratio L/G (水气比)|Rating Factor(选定系数）|相当参考标准点的冷却能力m3/h|系统推荐使用型号标准能力m3/h|Select Reserve（所选型号裕量）%";
            string[] titleArr = titles.Split('|');
            int i = 1;
            foreach (string item in titleArr)
            {
                dataSource.Add(new InputPara
                {
                    ID = i,
                    ParaName = item,
                });
                i++;
            }
            metroGridMain.DataSource = dataSource;
            metroGridMain.Columns[0].Width = 36;
            metroGridMain.Columns[1].Width = 260;
            metroGridMain.Columns[2].Width = 100;
            metroGridMain.Columns[3].Width = 100;
            metroGridMain.Columns[4].Width = 80;
            metroGridMain.Columns[1].ReadOnly = true;
            metroGridMain.Columns[3].ReadOnly = true;
            metroGridMain.Columns[4].ReadOnly = true;
            metroGridMain.Columns[1].HeaderText = "Mixed flow,Closed Cooling tower \r\n (复合流闭式冷却塔)";
            metroGridMain.Columns[2].HeaderText = "设计条件";
            metroGridMain.Columns[3].HeaderText = "转换值";
            metroGridMain.Columns[4].HeaderText = "单位";

            for(int j = 0; j < 4; j++)
            {
                metroGridMain.Rows[j].Cells[2].Style.BackColor = Color.FromArgb(153, 204, 0);
            }
        }

        private void metroButtonOK_Click(object sender, EventArgs e)
        {
            InputParam param = new InputParam();
            FinalResult finalResult = new FinalResult();
            CalcuModel cm = new CalcuModel();

            List<t_equparams> equParams = new List<t_equparams>();
            //dataSource取选型系数表所有数据
            List<t_equparams> dataSource = new List<t_equparams>();
            //dataSource取选型系数表所有数据
            List<t_equipment> equipments = new List<t_equipment>();

            #region 第一步
            double suanfa1 = 28;
            double f_range = MathEx.RoundDown(param.C10, 0);
            double f_approch = MathEx.RoundDown(param.C7, 0)- MathEx.RoundDown(param.C8, 0);
            double trueRange = Math.Round(param.C6 - param.C7, 1);
            List<t_equparams> table1 = CreateEquParams(suanfa1, f_range, f_approch, trueRange, dataSource);

            double f_approch2 = MathEx.RoundDown((param.C7 - param.C8), 0);
            List<t_equparams> table2 = CreateEquParams(suanfa1, f_range, f_approch2, trueRange, dataSource);

            double f_approch3 = f_approch2 + 1;
            List<t_equparams> table3 = CreateEquParams(suanfa1, f_range, f_approch3, trueRange, dataSource);

            equParams.Add(table1.FirstOrDefault(o => o.intro == "实际温降"));
            equParams.Add(table2.FirstOrDefault(o => o.intro == "实际温降"));
            equParams.Add(table3.FirstOrDefault(o => o.intro == "实际温降"));

            #endregion
            cm.B36 = param.C8;
            cm.B31 = Math.Round(param.C7 - param.C8, 1);
            cm.C32 = 28 * 10000 + Math.Round(param.C6 - param.C7, 1) * 1000 + cm.B32 * 10;
            cm.C33 = 28 * 10000 + Math.Round(param.C6 - param.C7, 1) * 1000 + cm.B33 * 10;
            //cm.D37 = 5 + (cm.B37 - 12);
            cm.I32 = GetWetBulbTemp(cm.B37, cm.C32, equParams);
            cm.I33 = 28 * 10000 + Math.Round(param.C6 - param.C7, 1) * 1000 + cm.B33 * 10;

            cm.I31 = cm.I32 + (cm.B31 - cm.B32) / (cm.B33 - cm.B32) * (cm.I33 - cm.I32);
            cm.M33 = GetWetBulbTemp(cm.B37 + 1, cm.C33, equParams);
            cm.M32 = GetWetBulbTemp(cm.B37 + 1, cm.C32, equParams);
            cm.M31 = cm.M32 + (cm.B31 - cm.B32) / (cm.B33 - cm.B32) * (cm.M33 - cm.M32);
            finalResult.value = cm.I31 + (cm.B36 - cm.B37) / (cm.B41 - cm.B37) * (cm.M31 - cm.I31);

            param.C13 = finalResult.value;

            foreach (var equ in equipments)
            {
                equ.enabled = equ.f_CTIDealWaterFlow >= param.C14 ? 1 : 0;
            }
            var minEqu = equipments.Where(o => o.enabled == 1).OrderBy(o => o.filterIndex).FirstOrDefault();
            param.C12 = (param.C5 * 1000) / (minEqu.f_m3h * 1.293);
            param.C15 = minEqu.filterIndex;
            param.SelectedFBSModel = minEqu.f_FBSModel;
            param.SelectedModel = minEqu.f_Model;
        }

        private List<t_equparams> CreateEquParams(double suanfa1, double f_range, double f_approch , double trueRange,List<t_equparams> dataSource)
        {
            List<t_equparams> result = new List<t_equparams>();
            var obj1 = dataSource.FirstOrDefault(o => o.suanfa1 == suanfa1 && o.f_range == f_range && o.f_approch == f_approch);
            if (obj1 != null)
            {
                obj1.intro = "处理为整数";
                result.Add(obj1);
            }

            var obj2 = dataSource.FirstOrDefault(o => o.suanfa1 == suanfa1 && o.f_range == trueRange && o.f_approch == f_approch);
            if (obj2 != null)
            {
                obj2.intro = "实际温降";
                result.Add(obj2);
            }

            var obj3 = dataSource.FirstOrDefault(o => o.suanfa1 == suanfa1 && o.f_range == f_range + 1 && o.f_approch == f_approch);
            if (obj3 != null)
            {
                obj3.intro = "温降升一位";
                result.Add(obj3);
            }

            return result;
        }
        /// <summary>
        /// 根据综合标识和逼近度获取湿球温度值
        /// </summary>
        /// <param name="tmp"></param>
        /// <param name="zonghe"></param>
        /// <param name="equParams"></param>
        /// <returns></returns>
        private double GetWetBulbTemp(double tmp,double zonghe, List<t_equparams> equParams)
        {
            double result = 0;
            var dataRow = equParams.FirstOrDefault(o => o.zonghe == zonghe);
            foreach (System.Reflection.PropertyInfo p in dataRow.GetType().GetProperties())
            {
                if (p.Name == "f_WetBulbTemp" + tmp)
                {
                    result = Convert.ToDouble(p.GetValue(dataRow));
                    break;
                }
            }
            return result;
        }

        private void metroButton2_Click(object sender, EventArgs e)
        {
            string str = "a1某某某";
            var aa = System.Text.Encoding.UTF8.GetBytes(str).Length;
            var bbb = str.Length;
        }
    }
}
